package com.zcy.auth.controller;

import com.zcy.auth.domain.Permission;
import com.zcy.auth.service.IPermissionService;
import com.zcy.basic.annotation.PermissionApi;
import com.zcy.basic.util.AjaxResult;
import com.zcy.basic.util.PageInfo;
import com.zcy.basic.util.PageList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/permission" )
@Api(value = "权限相关API", description = "权限的增删改查" )
@PermissionApi(name = "权限管理", description = "权限相关的增删改查")
public class PermissionController {
    @Autowired
    public IPermissionService permissionService;


    /**
     * 保存和修改公用的
     * @param permission 传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    @ApiOperation("保存或修改权限")
    @PermissionApi(name = "保存或修改权限" )
    public AjaxResult savePermission(@RequestBody Permission permission) {
        try {
            if (permission.getId() != null)
                permissionService.update(permission);
            else
                permissionService.add(permission);
            return AjaxResult.success("保存成功" );
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存失败" );
        }
    }

    /**
     * 删除权限对象信息
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    @ApiOperation("通过Id删除权限")
    @PermissionApi(name = "通过Id删除权限")
    public AjaxResult deletePermissionById(@PathVariable("id") Long id) {
        try {
                permissionService.deleteById(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /**
     * 通过Id获取 权限 实体
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("通过Id获取权限")
    @PermissionApi(name = "通过Id获取权限")
    public AjaxResult getPermissionById(@PathVariable("id") Long id) {
        try {
            Permission permission =permissionService.queryById(id);
            return AjaxResult.success(permission);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }


    /**
     * 查看所有的权限实体信息
     * @return
     */
    @GetMapping
    @ApiOperation("查询所有权限")
    @PermissionApi(name = "查询所有权限")
    public AjaxResult listAllPermission() {
        try {
            List<Permission> list = permissionService.listAll();
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }


    /**
    * 分页查询数据
    *
    * @param pageInfo 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    @ApiOperation("分页查询权限")
    @PermissionApi(name = "分页查询权限")
    public AjaxResult queryPermissionPage(@RequestBody PageInfo pageInfo) {
        try {
            PageList<Permission> pageList = permissionService.queryPage(pageInfo);
            return AjaxResult.success(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("分页查询失败" );
        }
    }

    /**
     * 批量删除
     * @param ids 权限ID数组
     * @return AjaxResult
     */
    @PatchMapping
    @ApiOperation("批量删除权限")
    @PermissionApi(name = "批量删除权限")
    public AjaxResult deleteBatchPermissionByIds(@ApiParam("permissionID数组" ) @RequestBody List<Long> ids) {
        try {
            permissionService.deleteBatch(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @GetMapping("/tree")
    @ApiOperation("获取权限树")
    public AjaxResult getPermissionTree() {
        try {
            List<Permission> permissionTree = permissionService.getPermissionTree();
            return AjaxResult.success(permissionTree);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @GetMapping("/tree/{id}")
    @ApiOperation("获取当前角色的权限树")
    public AjaxResult getRolePermissionTree(@PathVariable(value = "id") Long roleId) {
        try {
            List<Permission> permissionTree = permissionService.getRolePermissionTree(roleId);
            return AjaxResult.success(permissionTree);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @GetMapping("/ids/{id}")
    @ApiOperation("获取当前角色的权限列表")
    public AjaxResult getPermissionIdsByRoleId(@PathVariable(value = "id") Long roleId) {
        try {
            List<Long> permissionIds = permissionService.getPermissionIdsByRoleId(roleId);
            return AjaxResult.success(permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
}
